home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 247_01 / big.hpp < prev    next >
Text File  |  1989-04-17  |  9KB  |  197 lines

  1. /*
  2.  *    Experimental MIRACL  C++ Header file
  3.  *
  4.  *    AUTHOR  :    N.Coghlan
  5.  *                 Modified by M.Scott
  6.  *    NAME    :    big.hpp
  7.  *             
  8.  *    PURPOSE :    Definition of class Big
  9.  */
  10.  
  11. overload pow;
  12. overload sqrt;
  13. overload abs;
  14.  
  15. overload prime;
  16. overload gcd;
  17. overload root;
  18. overload inverse;
  19. overload rand;
  20.  
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #include "miracl.h"
  25.  
  26. class miracl
  27. { /* dummy class to initialise MIRACL */
  28. public:
  29.     miracl(int nd,small nb=MAXBASE) {mirsys(nd,nb);STROUT=TRUE;POINT=TRUE;}
  30. };
  31.  
  32. class Big 
  33.     big fn;
  34. public:
  35.     Big()                                               { fn = mirvar(0); } 
  36.     Big(int i)                                          { fn = mirvar(i); }
  37.     Big(long lg)                         { fn = mirvar(0); lconv(lg,fn);  }
  38.     Big(Big& b)                          { fn = mirvar(0); copy(b.fn,fn); }
  39.     Big(char* s)           {fn=mirvar(0);strcpy(IBUFF,s);cinnum(fn,stdin);}
  40.  
  41.     Big& operator=(int i)                    {convert(i,fn); return *this;}
  42.     Big& operator=(const Big& b)             {copy(b.fn,fn); return *this;}
  43.     Big& operator=(char* s){strcpy(IBUFF,s);cinnum(fn,stdin);return *this;}
  44.  
  45.     Big& operator++()                        {incr(fn,1,fn); return *this;}
  46.     Big& operator--()                        {decr(fn,1,fn); return *this;}
  47.     Big& operator+=(int i)                   {incr(fn,i,fn); return *this;}
  48.     Big& operator+=(const Big& b)          {add(fn,b.fn,fn); return *this;}
  49.  
  50.     Big& operator-=(int i)                   {decr(fn,i,fn); return *this;}
  51.     Big& operator-=(const Big& b)     {subtract(fn,b.fn,fn); return *this;}
  52.  
  53.     Big& operator*=(int i)                {premult(fn,i,fn); return *this;}
  54.     Big& operator*=(const Big& b)     {multiply(fn,b.fn,fn); return *this;}
  55.  
  56.     Big& operator/=(int i)              {subdiv(fn,i,fn);    return *this;}
  57.     Big& operator/=(const Big& b)       {divide(fn,b.fn,fn); return *this;}
  58.  
  59.     Big& operator%=(int i)     {convert(subdiv(fn,i,fn),fn); return *this;}
  60.     Big& operator%=(const Big& b)     {divide(fn,b.fn,b.fn); return *this;}
  61.  
  62.     friend Big operator-(const Big&);
  63.  
  64.     friend Big operator+(const Big&,int);
  65.     friend Big operator+(int,const Big&);
  66.     friend Big operator+(const Big&, const Big&);
  67.  
  68.     friend Big operator-(const Big&, int);
  69.     friend Big operator-(int,const Big&);
  70.     friend Big operator-(const Big&, const Big&);
  71.  
  72.     friend Big operator*(const Big&, int);
  73.     friend Big operator*(int,const Big&);
  74.     friend Big operator*(const Big&, const Big&);
  75.  
  76.     friend Big operator/(const Big&, int);
  77.     friend Big operator/(int,const Big&);
  78.     friend Big operator/(const Big&, const Big&);
  79.  
  80.     friend int operator%(const Big&, int);
  81.     friend Big operator%(const Big&, const Big&);
  82.  
  83.     friend bool operator<=(const Big& b1, const Big& b2)
  84.              {if (compare(b1.fn,b2.fn)<=0) return TRUE; else return FALSE;}
  85.     friend bool operator<=(const Big&,int);
  86.     friend bool operator<=(int,const Big&);
  87.  
  88.     friend bool operator>=(const Big& b1, const Big& b2)
  89.              {if (compare(b1.fn,b2.fn)>=0) return TRUE; else return FALSE;}
  90.     friend bool operator>=(const Big&,int);
  91.     friend bool operator>=(int,const Big&);
  92.     
  93.     friend bool operator==(const Big& b1, const Big& b2)
  94.              {if (compare(b1.fn,b2.fn)==0) return TRUE; else return FALSE;}
  95.     friend bool operator==(const Big&,int);
  96.     friend bool operator==(int,const Big&);
  97.  
  98.     friend bool operator!=(const Big& b1, const Big& b2)
  99.              {if (compare(b1.fn,b2.fn)!=0) return TRUE; else return FALSE;}
  100.     friend bool operator!=(const Big&, int);
  101.     friend bool operator!=(int, const Big&);
  102.  
  103.     friend bool operator<(const Big& b1, const Big& b2)
  104.               {if (compare(b1.fn,b2.fn)<0) return TRUE; else return FALSE;}
  105.     friend bool operator<(const Big&, int);
  106.     friend bool operator<(int, const Big&);
  107.  
  108.     friend bool operator>(const Big& b1, const Big& b2)
  109.               {if (compare(b1.fn,b2.fn)>0) return TRUE; else return FALSE;}
  110.     friend bool operator>(const Big&, int);
  111.     friend bool operator>(int, const Big&);
  112.  
  113.     friend Big sqrt(const Big&);
  114.     friend Big abs(const Big&);
  115.     friend bool prime(const Big& b)                   {return prime(b.fn);}
  116.     friend Big gcd(const Big&, const Big &);
  117.     friend Big pow(const Big&,int);
  118.     friend Big pow(const Big&, int, const Big&);
  119.     friend Big pow(const Big&, const Big&, const Big&);
  120.     friend Big inverse(const Big&, int);
  121.     friend Big inverse(const Big&, const Big&);
  122.     friend Big root(const Big&, int);
  123.     friend Big rand(const Big&);
  124.     friend istream& operator>>(istream& s, Big& x)
  125.                            { s >> IBUFF; cinnum(x.fn,stdin) ;   return s; }
  126.     friend ostream& operator<<(ostream& s, Big& x)
  127.                  { cotnum(x.fn,stdout); if (STROUT) s << OBUFF;  return s;}
  128.  
  129.     ~Big() {free((char *)fn); }
  130. };
  131.  
  132. Big operator-(const Big& b)   {Big nb; negate(b.fn,nb.fn); return nb;}
  133. Big operator+(const Big& b,int i)
  134.                               {Big abi; incr(b.fn, i, abi.fn); return abi;}
  135. Big operator+(int i,const Big& b)
  136.                               {Big aib; incr(b.fn, i, aib.fn); return aib;}
  137. Big operator+(const Big& b1, const Big& b2)
  138.                               {Big abb;add(b1.fn,b2.fn,abb.fn);return abb;}
  139.  
  140. Big operator-(const Big& b, int i)
  141.                               {Big mbi; decr(b.fn, i, mbi.fn); return mbi;}
  142. Big operator-(int i,const Big& b)
  143.           {Big mib;decr(b.fn, i, mib.fn);negate(mib.fn,mib.fn);return mib;}
  144. Big operator-(const Big& b1, const Big& b2)
  145.                        {Big mbb; subtract(b1.fn,b2.fn,mbb.fn); return mbb;}
  146.  
  147. Big operator*(const Big& b, int i)
  148.                            {Big xbi; premult(b.fn, i, xbi.fn); return xbi;}
  149. Big operator*(int i,const Big& b)
  150.                            {Big xib; premult(b.fn, i, xib.fn); return xib;}
  151. Big operator*(const Big& b1, const Big& b2)
  152.                        {Big xbb; multiply(b1.fn,b2.fn,xbb.fn); return xbb;}
  153.  
  154. Big operator/(const Big& b, int i)
  155.                             {Big dbi; subdiv(b.fn, i, dbi.fn); return dbi;}
  156. Big operator/(int i,const Big& b)
  157.          {Big dib;convert(i,dib.fn);divide(dib.fn,b.fn,dib.fn);return dib;}
  158. Big operator/(const Big& b1, const Big& b2)
  159.                          {Big dbb; divide(b1.fn,b2.fn,dbb.fn); return dbb;}
  160.  
  161. int operator%(const Big& b, int i)
  162.                                {Big mdbi; return(subdiv(b.fn,i, mdbi.fn));}
  163. Big operator%(const Big& b1, const Big& b2)
  164.                      {Big mdbb=b1;divide(mdbb.fn,b2.fn,b2.fn);return mdbb;}
  165. Big sqrt(const Big& b)              {Big z; root(b.fn, 2, z.fn); return z;}
  166. Big abs(const Big& b)               {Big z; absol(b.fn,z.fn);    return z;}
  167. Big gcd(const Big& b1,const Big& b2){Big z;gcd(b1.fn,b2.fn,z.fn);return z;}
  168. Big pow(const Big& b,int n)     {Big z; power(b.fn,n,z.fn,z.fn); return z;}
  169. Big pow(const Big& b1,int n,const Big& b3)
  170.                               {Big z; power(b1.fn,n,b3.fn,z.fn); return z;}
  171. Big pow(const Big& b1,const Big& b2,const Big& b3)
  172.                          {Big z; powmod(b1.fn,b2.fn,b3.fn,z.fn); return z;}
  173. Big inverse(const Big& b1,const Big& b2)
  174.                         {Big z; xgcd(b1.fn,b2.fn,z.fn,z.fn,z.fn);return z;}
  175. Big inverse(const Big& b,int n)
  176.                         {Big z=n; xgcd(b.fn,z.fn,z.fn,z.fn,z.fn);return z;}
  177. Big root(const Big& b,int n)          {Big z; root(b.fn,n,z.fn); return z;}
  178. Big rand(const Big& b)               {Big z; bigrand(b.fn,z.fn); return z;}
  179.  
  180.  
  181.  
  182. bool operator<=(const Big& b, int i)              {Big z=i; return (b<=z);}
  183. bool operator<=(int i, const Big& b)              {Big z=i; return (z<=b);}
  184. bool operator>=(const Big& b, int i)              {Big z=i; return (b>=z);}
  185. bool operator>=(int i, const Big& b)              {Big z=i; return (z>=b);}
  186. bool operator==(const Big& b, int i)              {Big z=i; return (b==z);}
  187. bool operator==(int i, const Big& b)              {Big z=i; return (z==b);}
  188. bool operator!=(const Big& b, int i)